/**
 * Created by Windows on 2015/11/20.
 */

$(function(){
    $(".business_id").on("change",function(){
        var business_id = $(this).val();
        $.ajax({
            url:"/json/getbusinessjson",
            data:{
                business_id:business_id
            },
            type:'post',
            success:function(data){
                if(data !=''){
                    $(".business_phone").val(data.business_phone);
                    $(".contacts").val(data.contacts);
                    $(".mobile").val(data.mobile);
                }
            },
            dataType:'json'
        })
    });

    $(".shop_next_product").on("click",function(){
        var business_id = $(".business_id").find("option:selected").val();
        var business_name = $.trim($(".business_name").val());
        var business_phone = $.trim($(".business_phone").val());
        var contacts = $.trim($(".contacts").val());
        var mobile = $.trim($(".mobile").val());
        var regBox = {
            regEmail:/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
            regName : /^[a-zA-Z0-9_-]{3,16}$/,//验证数字字母下划线
            regCh : /^[\u4e00-\u9fa5]{0,}$/,//验证汉子
            regMobile : /^0?1[3|4|5|7|8][0-9]\d{8}$/,//手机
            regTel : /^0[\d]{2,3}-[\d]{7,8}$/
        }

        if((!business_name)&& (!business_id)){
            alert("请填写公司名称！");return false;
        }

        if(business_name){
            var reg = regBox.regCh.test(business_name);
            if(!reg){
                alert("公司名称格式不正确！");return false;
            }
        }

        if(!contacts){
            alert("请填写联系人！");return false;
        }

        var mcontacts = regBox.regCh.test(contacts);
        if(!mcontacts){
            alert("联系人格式不正确！");return false;
        }

        if(!mobile){
            alert("请填写手机号码！");return false;
        }

        var mmobile = regBox.regMobile.test(mobile);
        if(!mmobile){
            alert("手机号码格式不正确！");return false;
        }

        $.ajax({
            url:"/json/checkbusinessjson",
            data:{
                business_id:business_id,
                business_name:business_name,
                business_phone:business_phone,
                contacts:contacts,
                mobile:mobile,
            },
            type:'post',
            success:function(data){
                var code = data.code;
                if(code==99){
                    alert("该报价产品存在问题,暂时无法认购,请联系客服解决");return false;
                }
                if(code==1){
                    alert("公司未填写或联系客服解决");return false;
                }
                if(code==2){
                    alert("公司名称已经存在，请换一个");return false;
                }
                if(code==8){
                    window.location.href = "/shopcart/product";
                }

            },
            dataType:'json'
        })

    });

    //这边修改配送方式
    $(".delivery_method").on("click",function(){
        var delivery_method = $(this).val();
        var delivery_addr = $(".delivery_addr").val();
        $.ajax({
            url:"/shopcart/delivery",
            data:{
                delivery_method:delivery_method
            },
            type:'post',
            dataType:'json',
            success:function(data){

            }
        });
    });

    $(".delivery_addr").on("change",function(){
        var delivery_addr = $(this).val();
        $.ajax({
            url:"/shopcart/delivery_addr",
            data:{
                delivery_addr:delivery_addr
            },
            type:'post',
            dataType:'json',
            success:function(data){

            }
        });
    });


    //修改数量
    $(".st_num").on("change",function(){
        var sj_num = $(".sj_num").val()*1;
        var sj_price = $(".sj_price").val()*1;
        var st_num = $.trim($(this).val())*1;

        var regBox = {
            regnum:/^\d+(\.\d+)?$/,
        }
        if(!st_num || st_num<0){
            alert("请填写认购数量！");
            $(this).val("5");
            $(".total").html(sj_price*5);
            $(".zoltotal").html(sj_price*5);
            return false;
        }
        var mst_num = regBox.regnum.test(st_num);
        if(!mst_num){
            alert("认购数量不正确！");
            $(this).val("5");
            $(".total").html(sj_price*5);
            $(".zoltotal").html(sj_price*5);
            return false;
        }

        if(st_num>sj_num){
            alert("对不起，你认购数量已超实际报价数量！");
            $(this).val(sj_num);
            $(".total").html(sj_price*sj_num);
            $(".zoltotal").html(sj_price*sj_num);
            return false;
        }
        $(".total").html(sj_price*st_num);
        $(".zoltotal").html(sj_price*st_num);

    });

    $(".create_order").on("click",function(){
        var st_num = $.trim($(".st_num").val())*1;
        var delivery_method = $(".delivery_method:checked").val();
        var delivery_addr = $(".delivery_addr").val();

        if(!delivery_method || delivery_method==''){
            alert("对不起，请选择配送方式不能为空！"); return false;
        }

        if(delivery_method==2){
            if(delivery_addr==''){
                alert("对不起，你的配送地址不能为空！"); return false;
            }
        }
        $.ajax({
            url:"/shopcart/updatenum",
            data:{
                product_num:st_num
            },
            type:'post',
            dataType:'json',
            success:function(data){
                if(data.order_sku['num']==st_num){
                    window.location = "/shopcart/createorder";
                }
            }
        });

    });

});


